//
//  HTNetworkService.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/10.
//

import Foundation
import Moya

enum HTEnumNetworkService {
    case htEnumHomeData(var_id: String, var_p1: String)
    case htEnumSubjectList(var_id: String, var_page: String, var_page_size: String, var_orderby: String, var_type: String, var_genre: String, var_g2: String, var_pubdate: String)
    case htEnumShareConfig(var_p1: String, var_p2: String)
    case htEnumPopularList(var_type: String)
    case htEnumSearch(var_keyword: String, var_page: String, var_page_size: String, var_p1: String)
    case htEnumAdConfig
    case htEnumSearchAssociationalWord(var_keyword: String)
    case htEnumFeedback(var_content: String, var_mail: String)
    case htEnumAnimeInfo(var_id: String, var_uid: String)
    case htEnumAnimeUrl(var_id: String, var_sig: String, var_unixtime: String)
    case htEnumAnimeSubtitle(var_id: String, var_type: String)
    case htEnumSubscribeBind(var_pid: String, var_receipt: String)
    case htEnumSubscribeRestore(var_pid: String, var_receipt: String)
    // var_uid 用户ID，没有传0
    case htEnumSubscribe(var_uid: String, var_p1: String)
    // 埋点
    case htEnumHomeMShNew(var_loadsuccess: String = "", var_errorinfo: String = "", var_like_list: String = "") // 首页展示
    case htEnumMomeMClNew(var_kid: String = "", var_c_id: String = "", var_c_name: String = "", var_secname: String = "", var_secid: String = "") // 首页点击
    case htEnumSearchShNew // 搜索中间页展示
    case htEnumSearchClNew(var_kid: String = "", var_word: String = "", var_type: String = "") // 搜索中间页点击
    case htEnumSearchResultShNew(var_type: String = "", var_result: String = "", var_keyword: String = "", var_errorinf: String = "") // 搜索结果页展示
    case htEnumSearchResultClNew(var_kid: String = "", var_item_id: String = "", var_type: String = "", var_word: String = "") // 搜索结果页点击
    case htEnumVipSh(var_source: String = "") // vip页展示
    case htEnumVipCl(var_kid: String = "", var_source: String = "") // vip页点击
    case htEnumSubscribeStatus(var_type: String = "", var_status: String = "", var_status2: String = "", var_source: String = "", var_pay_time: String = "", var_g_status: String = "", var_s_s_status: String = "", var_c_type: String = "", var_type2: String = "") // 订阅状态
    case htEnumPushCl(var_flag: String = "", var_jobid: String = "", var_text: String = "", var_tab: String = "") // 通知栏点击
    case htEnumMovielistSh(var_movielist_id: String = "") // 电影列表页展示
    case htEnumMovielistCl(var_movielist_id: String = "", var_movie_id: String = "", var_movie_name: String = "") // 电影列表页点击
    case htEnumPlayFinished(var_error_info: String = "", var_youtubeid: String = "", var_eps_id: String = "", var_play_url: String = "", var_m_type: String = "") // exoplayer播放器播放上报
    case htEnumMoviePlaySh(var_movie_id: String = "", var_movie_name: String = "", var_source: String = "", var_eps_id: String = "", var_eps_name: String = "", var_movielist_id: String = "", var_status: String = "", var_m_type: String = "") // 播放页展示
    case htEnumMoviePlayCl(var_movie_id: String = "", var_movie_name: String = "", var_kid: String = "", var_source: String = "", var_eps_id: String = "", var_eps_name: String = "", var_movielist_id: String = "", var_secname: String = "", var_subtitle: String = "", var_m_type: String = "", var_ori_type: String = "") // 播放页点击
    case htEnumMoviePlayLen(var_movie_id: String = "", var_movie_name: String = "", var_eps_id: String = "", var_eps_name: String = "", var_watch_len: String = "", var_source: String = "", var_if_success: String = "", var_wait_len: String = "", var_link_len: String = "", var_server_len: String = "", var_cache_len: String = "", var_lname: String = "", var_firsttime: String = "", var_lagtimes_server: String = "", var_if_first: String = "") // 播放页停留时长
}

// MARK: - TargetType Protocol Implementation

extension HTEnumNetworkService: TargetType {
    var baseURL: URL {
        switch self {
        case .htEnumSearchAssociationalWord:
            return URL(string: HTConfig.STATIC_queriesUrl)!
        case
            .htEnumHomeData(_, _),
            .htEnumSubjectList(_, _, _, _, _, _, _, _),
            .htEnumShareConfig(_, _),
            .htEnumPopularList(_),
            .htEnumSearch(_, _, _, _),
            .htEnumAdConfig,
            .htEnumFeedback(_, _),
            .htEnumAnimeInfo(_, _),
            .htEnumAnimeUrl(_, _, _),
            .htEnumAnimeSubtitle(_, _),
            .htEnumSubscribeBind(_, _),
            .htEnumSubscribeRestore:
            return URL(string: HTConfig.STATIC_baseUrl)!
        default:
            return URL(string: HTConfig.STATIC_buriedUrl)!
        }
    }

    var path: String {
        switch self {
        case .htEnumHomeData:
            return "/264/"
        case .htEnumSubjectList:
            return "/267/"
        case .htEnumShareConfig:
            return "/78/"
        case .htEnumPopularList:
            return "/290/"
        case .htEnumSearch:
            return "/289/"
        case .htEnumAdConfig:
            return "/84/"
        case .htEnumSearchAssociationalWord:
            return "/complete/search"
        case .htEnumFeedback:
            return "/5/"
        case .htEnumAnimeInfo:
            return "/270/"
        case .htEnumAnimeUrl:
            return "/271/"
        case .htEnumAnimeSubtitle:
            return "/163/"
        case .htEnumSubscribeBind:
            return "/325/"
        case .htEnumSubscribeRestore:
            return "/326/"
        case .htEnumSubscribe:
            return "/300/"
        default:
            return "/data"
        }
    }

    var method: Moya.Method {
        switch self {
        case
            .htEnumHomeData(_, _),
            .htEnumSubjectList(_, _, _, _, _, _, _, _),
            .htEnumShareConfig(_, _),
            .htEnumPopularList(_),
            .htEnumSearch(_, _, _, _),
            .htEnumAdConfig,
            .htEnumFeedback(_, _),
            .htEnumAnimeInfo(_, _),
            .htEnumAnimeUrl(_, _, _),
            .htEnumAnimeSubtitle(_, _),
            .htEnumSubscribeBind(_, _),
            .htEnumSubscribe(_, _),
            .htEnumSubscribeRestore:
            return .post
        default:
            return .get
        }
    }

    var task: Task {
        let userExpiredDate = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_userExpiredDate)

        var var_params: [String: Any] = [:]
        var_params["apns_id"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_deviceToken) ?? "8a983582effc3d97a8a8333eeeb790e187c2caeb3c3f88a48cd18ae7c84a3d93"
        var_params["app_id"] = HTConfig.STATIC_AppID
        var_params["app_ver"] = HTTools.ht_getAppVersion()
        var_params["brand"] = UIDevice.current.model
        var_params["country"] = HTTools.ht_getDeviceLocale().var_country
        var_params["device"] = UIDevice.current.systemName
        var_params["deviceNo"] = HTTools.ht_getDeviceIdentifier()
        var_params["idfa"] = HTTools.ht_getDeviceIdentifier()
        var_params["imsi"] = HTTools.ht_getImsi()
        var_params["installTime"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_firstInstall) ?? ""
        var_params["lang"] = HTTools.ht_getCurrentLanguage()
        var_params["model"] = "iPhone10,3"
        var_params["os_ver"] = UIDevice.current.systemVersion
        var_params["r1"] = "100"
        var_params["reg_id"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_fcmToken) ?? ""
        var_params["resolution"] = HTTools.ht_getScreenResolution()
        var_params["simcard"] = HTTools.ht_getSimCardStatus()
        var_params["timezone"] = HTTools.ht_getTimeZone()
        var_params["token"] = "1"
        var_params["vp"] = userExpiredDate == nil ? "0" : "1"
        var_params["sig2"] = "jE9TwyaLoyzTk1tTkdeMvE1Vw1eTk1pmjewdkE2ewdvUkE9TkdzMoyzS"
        var_params["unixtime1"] = Date().timeStamp

        var var_buriedParams: [String: Any] = [:]
        var_buriedParams["xaid"] = HTTools.ht_getDeviceIdentifier()
        var_buriedParams["ver"] = HTTools.ht_getAppVersion()
        var_buriedParams["cl"] = HTTools.ht_getDeviceLocale().var_language
        var_buriedParams["osver"] = UIDevice.current.systemVersion
        var_buriedParams["brand"] = UIDevice.current.model
        var_buriedParams["model"] = UIDevice.current.model
        var_buriedParams["display"] = HTTools.ht_getScreenResolution()
        var_buriedParams["uptime"] = "\(Int(Date().timeIntervalSince1970))"
        var_buriedParams["prodid"] = "2"
        var_buriedParams["tmzone"] = TimeZone.current.identifier
        var_buriedParams["install_len"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_firstInstall) ?? ""
        var_buriedParams["install_len2"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_firstInstall) ?? ""
        var_buriedParams["data"] = "1"
        var_buriedParams["userid"] = "0"
        var_buriedParams["reg_id"] = UserDefaults().string(forKey: HTConfig.STATIC_userDefaults_fcmToken) ?? ""
        var_buriedParams["app_id"] = HTConfig.STATIC_AppID
        var_buriedParams["idfa"] = HTTools.ht_getDeviceIdentifier()
        var_buriedParams["project_id"] = "3"

        switch self {
        case let .htEnumSubscribe(var_uid, var_p1):
            var_params["var_uid"] = var_uid
            var_params["var_p1"] = var_p1
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumHomeData(var_id, var_p1):
            var_params["id"] = var_id
            var_params["p1"] = var_p1
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumSubjectList(var_id, var_page, var_page_size, var_orderby, var_type, var_genre, var_g2, var_pubdate):
            var_params["id"] = var_id
            var_params["page"] = var_page
            var_params["page_size"] = var_page_size
            var_params["orderby"] = var_orderby
            var_params["type"] = var_type
            var_params["genre"] = var_genre
            var_params["g2"] = var_g2
            var_params["pubdate"] = var_pubdate
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumShareConfig(var_p1, var_p2):
            var_params["p1"] = var_p1
            var_params["p2"] = var_p2
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumPopularList(var_type):
            var_params["type"] = var_type
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumSearch(var_keyword, var_page, var_page_size, var_p1):
            var_params["keyword"] = var_keyword
            var_params["page"] = var_page
            var_params["page_size"] = var_page_size
            var_params["p1"] = var_p1
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case .htEnumAdConfig:
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumSearchAssociationalWord(var_keyword):
            return .requestParameters(parameters: ["client": "youtube", "q": var_keyword], encoding: URLEncoding.queryString)
        case let .htEnumFeedback(var_content, var_mail):
            var_params["content"] = var_content
            var_params["mail"] = var_mail
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumAnimeInfo(var_id, var_uid):
            var_params["id"] = var_id
            var_params["uid"] = var_uid
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumAnimeUrl(var_id, var_sig, var_unixtime):
            var_params["id"] = var_id
            var_params["sig2"] = var_sig
            var_params["unixtime1"] = var_unixtime
            var_params["unixtime"] = var_unixtime
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumAnimeSubtitle(var_id, var_type):
            var_params["id"] = var_id
            var_params["type"] = var_type
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumSubscribeBind(var_pid, var_receipt):
            var_params["fid"] = "0"
            var_params["pid"] = var_pid
            var_params["flag"] = "1"
            var_params["receipt"] = var_receipt
            var_params["uid"] = "0"
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        case let .htEnumSubscribeRestore(var_pid, var_receipt):
            var_params["fid"] = "0"
            var_params["pid"] = var_pid
            var_params["flag"] = "0"
            var_params["receipt"] = var_receipt
            var_params["uid"] = "0"
            return .requestParameters(parameters: var_params, encoding: URLEncoding.default)
        // 埋点
        case let .htEnumHomeMShNew(var_loadsuccess, var_errorinfo, var_like_list): // 首页展示
            var_buriedParams["pointname"] = "home_m_sh_new"
            var_buriedParams["notify_status"] = "1"
            var_buriedParams["loadsuccess"] = var_loadsuccess
            var_buriedParams["errorinfo"] = var_errorinfo
            var_buriedParams["like_list"] = var_like_list
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMomeMClNew(var_kid, var_c_id, var_c_name, var_secname, var_secid): // 首页点击
            var_buriedParams["pointname"] = "home_m_cl_new"
            var_buriedParams["kid"] = var_kid
            var_buriedParams["c_id"] = var_c_id
            var_buriedParams["c_name"] = var_c_name
            var_buriedParams["secname"] = var_secname
            var_buriedParams["secid"] = var_secid
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case .htEnumSearchShNew:
            var_buriedParams["pointname"] = "search_sh_new"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumSearchClNew(var_kid, var_word, var_type):
            var_buriedParams["pointname"] = "search_cl_new"
            var_buriedParams["kid"] = var_kid
            var_buriedParams["word"] = var_word
            var_buriedParams["type"] = var_type
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumSearchResultShNew(var_type, var_result, var_keyword, var_errorinf):
            var_buriedParams["pointname"] = "search_result_sh_new"
            var_buriedParams["type"] = var_type
            var_buriedParams["result"] = var_result
            var_buriedParams["keyword"] = var_keyword
            var_buriedParams["errorinf"] = var_errorinf
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumSearchResultClNew(var_kid, var_item_id, var_type, var_word):
            var_buriedParams["pointname"] = "search_result_cl_new"
            var_buriedParams["kid"] = var_kid
            var_buriedParams["item_id"] = var_item_id
            var_buriedParams["type"] = var_type
            var_buriedParams["word"] = var_word
            var_buriedParams["card_type"] = ""
            var_buriedParams["if_tags"] = ""
            var_buriedParams["order"] = ""
            var_buriedParams["m_type"] = "3"
            var_buriedParams["status"] = ""
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumVipSh(var_source):
            var_buriedParams["pointname"] = "vip_sh"
            var_buriedParams["source"] = var_source
            var_buriedParams["pay_method"] = "2"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumVipCl(var_kid, var_source):
            var_buriedParams["pointname"] = "vip_cl"
            var_buriedParams["kid"] = var_kid
            var_buriedParams["source"] = var_source
            var_buriedParams["pay_type"] = "2"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumSubscribeStatus(var_type, var_status, var_status2, var_source, var_pay_time, var_g_status, var_s_s_status, var_c_type, var_type2):
            var_buriedParams["pointname"] = "subscribe_status"
            var_buriedParams["type"] = var_type
            var_buriedParams["status"] = var_status
            var_buriedParams["status2"] = var_status2
            var_buriedParams["source"] = var_source
            var_buriedParams["pay_time"] = var_pay_time
            var_buriedParams["g_status"] = var_g_status
            var_buriedParams["s_s_status"] = var_s_s_status
            var_buriedParams["fid"] = ""
            var_buriedParams["ctype"] = var_c_type
            var_buriedParams["type2"] = var_type2
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumPushCl(var_flag, var_jobid, var_text, var_tab):
            var_buriedParams["pointname"] = "push_cl"
            var_buriedParams["flag"] = var_flag
            var_buriedParams["jobid"] = var_jobid
            var_buriedParams["text"] = var_text
            var_buriedParams["tab"] = var_tab
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMovielistSh(var_movielist_id):
            var_buriedParams["pointname"] = "movielist_sh"
            var_buriedParams["movielist_id"] = var_movielist_id
            var_buriedParams["list_type"] = "5"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMovielistCl(var_movielist_id, var_movie_id, var_movie_name):
            var_buriedParams["pointname"] = "movielist_cl"
            var_buriedParams["movielist_id"] = var_movielist_id
            var_buriedParams["movie_id"] = var_movie_id
            var_buriedParams["movie_name"] = var_movie_name
            var_buriedParams["movie_type"] = "4"
            var_buriedParams["list_type"] = "5"
            var_buriedParams["kid"] = "1"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumPlayFinished(var_error_info, var_youtubeid, var_eps_id, var_play_url, var_m_type):
            var_buriedParams["pointname"] = "play_finished"
            var_buriedParams["error_info"] = var_error_info
            var_buriedParams["youtubeid"] = var_youtubeid
            var_buriedParams["eps_id"] = var_eps_id
            var_buriedParams["play_url"] = var_play_url
            var_buriedParams["m_type"] = var_m_type
            var_buriedParams["type"] = "6"
            var_buriedParams["source"] = "8"
            var_buriedParams["m_free"] = "\(HTTools.ht_getSystemMemory()!.freeMemory)"
            var_buriedParams["m_total"] = "\(HTTools.ht_getSystemMemory()!.totalMemory)"
            var_buriedParams["r_space"] = "\(HTTools.ht_getFreeDiskSpace())"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMoviePlaySh(var_movie_id, var_movie_name, var_source, var_eps_id, var_eps_name, var_movielist_id, var_status, var_m_type):
            var_buriedParams["pointname"] = "movie_play_sh"
            var_buriedParams["movie_id"] = var_movie_id
            var_buriedParams["movie_name"] = var_movie_name
            var_buriedParams["source"] = var_source
            var_buriedParams["eps_id"] = var_eps_id
            var_buriedParams["eps_name"] = var_eps_name
            var_buriedParams["movielist_id"] = var_movielist_id
            var_buriedParams["status"] = var_status
            var_buriedParams["m_type"] = var_m_type
            var_buriedParams["movie_type"] = "5"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMoviePlayCl(var_movie_id, var_movie_name, var_kid, var_source, var_eps_id, var_eps_name, var_movielist_id, var_secname, var_subtitle, var_m_type, var_ori_type):
            var_buriedParams["pointname"] = "movie_play_cl"
            var_buriedParams["movie_id"] = var_movie_id
            var_buriedParams["movie_name"] = var_movie_name
            var_buriedParams["kid"] = var_kid
            var_buriedParams["source"] = var_source
            var_buriedParams["eps_id"] = var_eps_id
            var_buriedParams["eps_name"] = var_eps_name
            var_buriedParams["movielist_id"] = var_movielist_id
            var_buriedParams["secname"] = var_secname
            var_buriedParams["subtitle"] = var_subtitle
            var_buriedParams["m_type"] = var_m_type
            var_buriedParams["ori_type"] = var_ori_type
            var_buriedParams["movie_type"] = "5"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        case let .htEnumMoviePlayLen(var_movie_id, var_movie_name, var_eps_id, var_eps_name, var_watch_len, var_source, var_if_success, var_wait_len, var_link_len, var_server_len, var_cache_len, var_lname, var_firsttime, var_lagtimes_server, var_if_first):
            var_buriedParams["pointname"] = "movie_play_len"
            var_buriedParams["movie_id"] = var_movie_id
            var_buriedParams["movie_name"] = var_movie_name
            var_buriedParams["eps_id"] = var_eps_id
            var_buriedParams["eps_name"] = var_eps_name
            var_buriedParams["watch_len"] = var_watch_len
            var_buriedParams["source"] = var_source
            var_buriedParams["if_success"] = var_if_success
            var_buriedParams["wait_len"] = var_wait_len
            var_buriedParams["link_len"] = var_link_len
            var_buriedParams["server_len"] = var_server_len
            var_buriedParams["cache_len"] = var_cache_len
            var_buriedParams["lname"] = var_lname
            var_buriedParams["firsttime"] = var_firsttime
            var_buriedParams["firsttime2"] = var_firsttime
            var_buriedParams["lagtimes"] = var_lagtimes_server
            var_buriedParams["lagtimes_server"] = var_lagtimes_server
            var_buriedParams["if_first"] = var_if_first
            var_buriedParams["movie_type"] = "4"
            return .requestParameters(parameters: var_buriedParams, encoding: URLEncoding.queryString)
        }
    }

    var headers: [String: String]? {
        return [:]
    }
}
